<?php
namespace app\common\logic;

use app\common\component\help_get\HelpGetService;
use app\common\component\help_to\HelpToService;
use app\common\component\user_wallet_silver\UserWalletSilverService;

class HelpGet {
	/*
	 * 确认收款订单
	 */
	public function confirmHelpGet($help_get_id) {
		$data = [
			'begin_time' => time(),
			'status' => 4
		];

		return $this -> doUpdate($data, $help_get_id);
	}

	/*
	 * 确认首次收款订单
	 */
	public function firstConfirmHelpGet($help_get_id) {
		$currentTime = time();
		$data = [
			'begin_time' => $currentTime,
			'end_time' => $currentTime + 48 * 60 * 60,
			'status' => 4
		];

		return $this -> doFirstUpdate($data, $help_get_id);
	}

	/*
	 * 确认未收款订单
	 */
	public function notConfirmHelpGet($help_get_id) {
		$data = [
			'begin_time' => time(),
			'status' => 3
		];

		return $this -> doUpdate($data, $help_get_id);
	}

	/*
	 * 首单未收款订单
	 */
	public function notFirstConfirmHelpGet($help_get_id) {
		$data = [
			'begin_time' => time(),
			'status' => 1
		];

		return $this -> doFirstUpdate($data, $help_get_id);
	}

	private function doUpdate($data, $help_get_id) {
		$helpGetService = new HelpGetService();
		$restulHelpGet = $helpGetService -> getHelpGet($help_get_id);
		if($restulHelpGet['code'] !== 0) {
			return $restulHelpGet;
		}

		$helpToId = $restulHelpGet['data']['help_get'] -> getData('help_to_id');

		$helpToService = new HelpToService();
		$restulHelpTo = $helpToService -> getHelpTo($helpToId);
		if($restulHelpTo['code'] != 0) {
			return $restulHelpTo;
		}

		//\think\Db::startTrans();
		try {
			if(!isset($data['end_time'])) {
				$data['end_time'] = $data['begin_time'] +  (getParam('MONEY_FREEZE_TIME') * 24 * 60 * 60);
			}

			$resultHelpTo = $helpToService -> update($data, $helpToId);
			if($resultHelpTo['code'] !== 0) {
				throw new \Exception($resultHelpTo['message']);
			}

			if($data['status'] == 4) {
				$userId = $restulHelpTo['data']['help_to'] -> getData('user_id');
				// 冻结本金与利息
				$resultUserWalletFreeze = (new UserWalletFreeze) -> doFreeze($userId, $restulHelpTo['data']['help_to'] -> money, true, '冻结打款帮助的本金与利息', 1, $data['end_time']);
				if($resultUserWalletFreeze['code'] !== 0) {
					throw new \Exception($resultUserWalletFreeze['message']);
				}
				
				$userWalletLogic = new UserWallet();
				$resultUserWalletDoReward = $userWalletLogic -> doReward($userId, $restulHelpTo['data']['help_to'] -> money);
				if($resultUserWalletDoReward['code'] !== 0) {
					throw new \Exception($resultUserWalletDoReward['message']);
				}

				$resultUserWalletWithdrawPosition = $userWalletLogic -> withdrawPosition($userId, $restulHelpTo['data']['help_to'] -> money);
				if($resultUserWalletWithdrawPosition['code'] !== 0) {
					throw new \Exception($resultUserWalletWithdrawPosition['message']);
				}

				$getUserHelpGet = \app\common\component\user\UserModel::get(['user_id' => $restulHelpGet['data']['help_get'] -> getData('user_id')]);
				winicSMS($getUserHelpGet -> phone, ['name' => $getUserHelpGet -> realname], 19);
			}

			unset($data['end_time']);
			$resultHelpGet = $helpGetService -> update($data, $help_get_id);
			if($resultHelpGet['code'] !== 0) {
				throw new \Exception($resultHelpGet['message']);
			}

			//\think\Db::commit();
		} catch(\Exception $e) {
			//\think\Db::rollback();

			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '收款成功',
			'data' => [
				'help_to' => $restulHelpTo['data']['help_to']
			]
		];
	}

	private function doFirstUpdate($data, $help_get_id) {
		\think\Db::startTrans();
		try {
			$resultUpdate = $this -> doUpdate($data, $help_get_id);
			if($resultUpdate['code'] != 0) {
				throw new \Exception($resultUpdate['message']);
			}

			if($data['status'] == 4) {
				$userId = $resultUpdate['data']['help_to'] -> getData('user_id');
				// 设置VIP
				$resultUserVip = (new UserVip()) -> setVip($userId);
				if($resultUserVip['code'] != 0) {
					throw new \Exception($resultUserVip['message']);
				}

				// 执行银豆发放
				$resultUserWalletSilver = (new UserWalletSilverService()) -> createLogV01($userId, 1, 3);
				if($resultUserWalletSilver['code'] != 0) {
					throw new \Exception('user_id:' . $getUser -> promotion_id . ', ' . $resultUserWalletSilver['message'] . '!');
				}

				// 备用金池
				$resultPettyCash = (new \app\common\logic\PettyCash()) -> add($userId);
				if($resultPettyCash['code'] != 0) {
					throw new \Exception($resultPettyCash['message']);
				}
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return [
				'code' => -1,
				'message' => $e -> getMessage()
			];
		}

		return [
			'code' => 0,
			'message' => '收款成功'
		];
	}
}
?>